package cn.lopr.im.api.controller;

import cn.lopr.im.api.annotation.Auth;
import cn.lopr.im.api.bean.User;
import cn.lopr.im.api.common.CallbackMessage;
import cn.lopr.im.api.configuration.AuthInfo;
import cn.lopr.im.api.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author shuangyang7
 * @date 2021/1/4 19:24
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private IMessageService messageService;

    @RequestMapping("/addMessage")
    public CallbackMessage<?> addMessage(cn.lopr.im.api.bean.Message message) {

        if (messageService.addMessage(message)) {
            return CallbackMessage.createBySuccess();
        }

        return CallbackMessage.createByError("请重试");
    }

    @Auth
    @RequestMapping("/getOfflineMessage")
    @ResponseBody
    public CallbackMessage<?> getOfflineMessage() {

        User user = AuthInfo.get();

        List<cn.lopr.im.api.bean.Message> notReadMessage = messageService.getNotReadMessage(user.getPhone());

        return CallbackMessage.createBySuccess("", notReadMessage);
    }

}
